package com.shou.infrastructure.persistent.repository.park;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shou.domain.common.PageQueryEntity;
import com.shou.domain.park.model.ParkingRecordEntity;
import com.shou.domain.park.model.ParkingRecordEntity;
import com.shou.domain.park.repository.IParkingRecordRepository;
import com.shou.infrastructure.persistent.dao.IParkingRecordDao;
import com.shou.infrastructure.persistent.dao.IParkingSpaceDao;
import com.shou.infrastructure.persistent.po.ParkingRecordPO;
import com.shou.infrastructure.persistent.repository.park.mapstruct.ParkingLotPOConvert;
import com.shou.infrastructure.persistent.repository.park.mapstruct.ParkingRecordPOConvert;
import jakarta.annotation.Resource;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;

/**
* @author Nemo
* @description 针对表【parkingrecords(停车记录表，记录车辆的进出信息)】的数据库操作Service
* @createDate 2025-02-07 22:10:51
*/
@Repository("parkingRecordRepository")
public class ParkingRecordRepository implements IParkingRecordRepository {
    @Resource
    private IParkingRecordDao parkingRecordDao;
    @Resource
    private ParkingRecordPOConvert parkingRecordPOConvert;
    @Override
    public void insert(ParkingRecordEntity parkingRecord) {
        ParkingRecordPO parkingRecordPO = parkingRecordPOConvert.convertParkingRecordEntityToPO(parkingRecord);
        try {
            parkingRecordDao.insertSelective(parkingRecordPO);
        } catch (DataIntegrityViolationException e) {
            Throwable rootCause = e.getRootCause();
            if (rootCause instanceof SQLException && "22001".equals(((SQLException) rootCause).getSQLState())) {
                throw new IllegalArgumentException("输入数据过长导致SQL执行失败: " + rootCause.getMessage());
            } else {
                throw new IllegalArgumentException(e.getMessage());
            }
        }
    }

    @Override
    public void updateParkingRecordById(ParkingRecordEntity parkingRecord) {
        ParkingRecordPO parkingRecordPO = parkingRecordPOConvert.convertParkingRecordEntityToPO(parkingRecord);
        parkingRecordDao.updateByPrimaryKeySelective(parkingRecordPO);
    }

    @Override
    public ParkingRecordEntity getParkingRecordById(Integer id) {
        ParkingRecordPO parkingRecordPO = parkingRecordDao.selectById(id);
        return parkingRecordPOConvert.convertParkingRecordPOToEntity(parkingRecordPO);
    }

    @Override
    public IPage<ParkingRecordEntity> getParkingRecordsPage(PageQueryEntity pageQueryEntity) {
        // 根据页码和页面大小,构建分页对象
        Page<ParkingRecordPO> page = new Page<>(pageQueryEntity.getPageIndex(), pageQueryEntity.getPageSize());
        // 采用模糊查询
        IPage<ParkingRecordPO> pageMessage = parkingRecordDao.selectPageMessageByCondition(
                page,
                pageQueryEntity.getUserId(),
                pageQueryEntity.getParkingSpaceCode(),
                pageQueryEntity.getStartTime(),
                pageQueryEntity.getEndTime(),
                pageQueryEntity.getCost(),
                pageQueryEntity.getStatus(),
                pageQueryEntity.getRemark(),
                pageQueryEntity.getSortName(),
                pageQueryEntity.getSort()
        );
        return parkingRecordPOConvert.convertParkingRecordPOPageToEntity(pageMessage);
    }
}
